package com.fau.sd.servlet;

import java.io.IOException;
import java.util.Date;
import java.util.Timer;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.fau.sd.timer.RemindTask;
import com.fau.sd.util.UuidUtil;

/**
 * @desc 话务分配、订单分配提醒话务员
 * @author kangyj
 * @datetime 2015年1月15日,上午10:58:24
 */
@WebServlet("/AllocateServlet")
public class AllocateServlet extends HttpServlet {
    
	private static final long serialVersionUID = -4401865262464872353L;
	
	private JdbcTemplate jdbcTarget;
	
    public AllocateServlet() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		String operatorId = request.getParameter("operatorId");
		String eid = request.getParameter("eid");
		String content = request.getParameter("content");
		
		ServletContext servletContext = this.getServletContext();  
        WebApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(servletContext);
        
        jdbcTarget = (JdbcTemplate) context.getBean("jdbcTarget");
        
        jdbcTarget.update("insert into medical_message(id, content, is_read, hospital_id, create_time, uid) values(?,?,?,?,?,?) "
        		, new Object[]{UuidUtil.getUuid(), content, 0, eid, new Date(), operatorId});
		this.startTask(operatorId, content);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}
	
	/**
	 * @desc 开启任务，提示话务员
	 * @param uid 话务员id
	 * @param content 提醒内容
	 * @author kangyj
	 * @datetime 2015年1月15日,上午11:08:56
	 */
	public void startTask(String uid, String content) {
		Timer timer = new Timer();
		timer.schedule(new RemindTask(uid, content), 0);
	}
}
